var Spawn = function(position, speed)
{
	this.x = position.x;
	this.y = position.y;
	this.speed = speed;
	this.target;
	this.hasTarget = false;
	this.color;
	this.id;
	this.width = 10;
	
	this.calculate = function(game)
	{
		for(i = 0; i < this.game.entities.length; i++)
		{
			i = String(i);
			if(this.getId() != this.game.entities[i].getId() && this.distanceTo(this.game.entities[i].getPosition()) <= this.speed+this.width)
			{
				console.log("Entity "+this.getId()+" colliding with "+this.game.entities[i].getId());
			}
		}
		
		if(this.hasTarget)
		{
			
			if(this.x > this.target.x)
			{
				this.x -= speed;
			}
			if(this.x < this.target.x)
			{
				this.x += speed;
			}
			if(this.y > this.target.y)
			{
				this.y -= speed;
			}
			if(this.y < this.target.y)
			{
				this.y += speed;
			}
		}
		
		if(	this.hasTarget && 
			(this.y >= this.target.y-speed && this.y <= this.target.y+speed) && 
			(this.x >= this.target.x-speed && this.x <= this.target.x+speed) 
		)
		{
			this.hasTarget = false;
			this.target = null;
		}
		
		
		
		
		if(!this.hasTarget)
		{
			this.setTarget(randomPosOnMap());
		}
	}
	
	this.draw = function(ctx)
	{
		ctx.fillStyle = this.color;
		ctx.beginPath();
		ctx.arc(this.x, this.y, this.width, 0, Math.PI*2, true);
		ctx.closePath();
		ctx.fill();
		
		if(this.hasTarget)
		{
			ctx.fillText("+", this.target.x-5, this.target.y);
		}
		
		ctx.fillStyle = "#000000";
		ctx.fillText(this.id, this.x, this.y);
	}
	
	this.distanceTo = function(pos)
	{
		var x_delta = Math.abs(this.x - pos.x);
		var y_delta = Math.abs(this.y - pos.x);
		
		var distance = Math.sqrt(x_delta*x_delta + y_delta*y_delta);
		
		return distance;
	}
	
	this.setTarget = function(position)
	{
		this.target = position;
		this.hasTarget = true;
	}
	
	this.setId = function(id)
	{
		this.id = id;
	}
	
	this.getId = function()
	{
		return this.id;
	}
	
	this.getPosition = function()
	{
		return new Position(this.x, this.y);
	}
	
	this.setGameObject = function(g)
	{
		this.game = g;
	}
		
}